# -*- mode: python; -*-

#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#

Import('BuildEnv')
env = BuildEnv.Clone()

def SrcBuildDir(x):
    return '#/' + Dir('.').path + '/../../' + x;

env.Prepend(LIBS=['io', 'base', 'sandesh'])
env.Append(LIBPATH=['.', SrcBuildDir('base'), SrcBuildDir('io')])
env.Append(CPPPATH = env['TOP'])

# libsandeshflow
SandeshGenFiles = env.SandeshGenCpp('flow.sandesh')
SandeshGenSrcs = env.ExtractCpp(SandeshGenFiles)
SandeshGenHdrs = env.ExtractHeader(SandeshGenFiles)
libsandeshflow = env.Library(target = 'sandeshflow', source = [SandeshGenSrcs,
                                                               'flow_sandesh_impl.cc'])
env.Install(env['TOP_LIB'], libsandeshflow)
env.Install(env['TOP_INCLUDE'] + '/sandesh/common', SandeshGenHdrs)

#libsandeshvns
SandeshGenFiles = env.SandeshGenCpp('vns.sandesh')
SandeshGenSrcs = env.ExtractCpp(SandeshGenFiles)
SandeshGenHdrs = env.ExtractHeader(SandeshGenFiles)
libsandeshvns = env.Library(target = 'sandeshvns', source = SandeshGenSrcs)
env.Install(env['TOP_LIB'], libsandeshvns)
env.Install(env['TOP_INCLUDE'] + '/sandesh/common', SandeshGenHdrs)

# Generate the Python source files
flow_pkg = env.SandeshGenPy('flow.sandesh', 'sandesh_common/', False)
vns_pkg = env.SandeshGenPy('vns.sandesh', 'sandesh_common/', False)

local_sources = [ 'setup.py' ]
local_sources_rules = []
for file in local_sources:
    local_sources_rules.append(env.Install(Dir('.'), '#controller/src/sandesh/common/' + file))

init_sources = [ '__init__.py' ]
init_sources_rules = []
for file in init_sources:
    init_sources_rules.append(env.Install(Dir('sandesh_common'), '#controller/src/sandesh/common/' + file))

sdist_depends = [local_sources_rules, init_sources_rules,
                 flow_pkg, vns_pkg]

cd_cmd = 'cd ' + Dir('.').path + ' && '
sdist_gen = env.Command('dist/sandesh-common-0.1dev.tar.gz',
                        'setup.py', cd_cmd + 'python setup.py sdist')

# install everything before building distribution
env.Depends(sdist_gen, sdist_depends)
env.Alias('nodemgr-full', sdist_gen)
env.Default(sdist_gen)

if 'install' in BUILD_TARGETS:
    install_cmd = env.Command(None, 'setup.py',
                              cd_cmd + 'python setup.py install %s' %
                              env['PYTHON_INSTALL_OPT'])
    env.Depends(install_cmd, sdist_depends)
    env.Alias('install', install_cmd)
